
$(function () {
    var selectCompanyList
    $.ajax({
        url: '/getAllCompanyName',
        type: 'get',
        success: function (data) {
            selectCompanyList = data
        }
    })

    layui.use(['form', 'layedit', 'laydate'], function () {
        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate;

        addOptions(selectCompanyList, "company")
        form.render()

        form.on('select(company)', function (data1) {
            $.ajax({
                url: '/getProject',
                type: 'get',
                data: {
                    companyname: data1.value
                },
                success: function (res) {

                    addOptions(res, "project")
                    form.render()
                }
            })
        });

        $.ajax({
            url: '/getMyUserInfo',
            type: 'get',
            data: {
                userNo: $.cookie("loginUserNo")
            },
            success: function (data) {
                if (data == '') {
                    layer.msg("您还没有录入个人信息，请录入！")
                    var cardno = getRandomCardNo()
                    submitUserInfo('/insertUserInfo',cardno,"","")

                } else {
                    layer.msg("您已经录入个人信息，可以修改！")
                    $.ajax({
                        url: '/getProject',
                        type: 'get',
                        data: {
                            companyname: data.company
                        },
                        success: function (res) {
                            addOptions2(res, "project", data.project)
                            form.render()
                        }
                    })
                    var sex
                    if (data.sex == 0) {
                        sex = "男"
                    } else {
                        sex = "女"
                    }

                    form.val('insertuserinfo', {
                        username: data.username,
                        sex: sex,
                        idcard: data.idcard,
                        worklevel: data.worklevel,
                        tel: data.tel,
                        wagescardno: data.wagescardno,
                        company: data.company,
                        project: data.project,
                        worktype: data.worktype,
                        grade: data.grade
                    });

                    submitUserInfo('/updateMyUserInfo', data.cardno,data.idcard,data.tel)
                }
            }
        })


        form.verify({
            telephone: [
                /^1[3-9]\d{9}$/
                , '请输入正确的手机号'
            ],
            idcard: [
                /^[1-9]\d{5}(18|19|2([0-9]))\d{2}(0[0-9]|10|11|12)([0-2][1-9]|30|31)\d{3}[0-9Xx]$/
                , '请输入正确的身份证'
            ],
            wagescard: [
                /^([1-9]{1})(\d{15}|\d{17})$/
                , '请输入正确的工资卡号'
            ]
        })
        //
        function submitUserInfo(str, cardNo,IDCard,Tel) {
            form.on('submit(insertuserinfo)', function (data) {
                layer.confirm('请确认您的信息是否正确？', {
                    btn: ['确认', '再看看'],
                    btn1: function () {
                        var sex
                        if (data.field.sex == "男") {
                            sex = 0
                        } else {
                            sex = 1
                        }
                        $.ajax({
                            url: '/checkIdCard',
                            type: 'post',
                            data: {
                                idcard: data.field.idcard
                            },
                            success: function (resIdCard) {
                                if (resIdCard == "" || (resIdCard == IDCard)) {
                                    $.ajax({
                                        url: '/checkTel',
                                        type: 'post',
                                        data: {
                                            tel: data.field.tel
                                        },
                                        success: function (resTel) {
                                            if (resTel == "" || (resTel == Tel)) {
                                                var loginId = $.cookie("loginUserNo")
                                                $.ajax({
                                                    url: str,
                                                    type: "post",
                                                    data: {
                                                        userno: loginId,
                                                        username: data.field.username,
                                                        sex: sex,
                                                        age: getAge(data.field.idcard),
                                                        idcard: data.field.idcard,
                                                        worklevel: data.field.worklevel,
                                                        tel: data.field.tel,
                                                        cardno: cardNo,
                                                        wagescardno: data.field.wagescardno,
                                                        project: data.field.project,
                                                        company: data.field.company,
                                                        worktype: data.field.worktype,
                                                        grade: data.field.grade
                                                    },
                                                    dataType: 'json',
                                                    success: function (data) {
                                                        layer.msg("提交成功！")
                                                        setTimeout(function () {
                                                            window.location.reload();
                                                        }, 1000)
                                                    }
                                                })
                                            } else {
                                                layer.msg("您输入的部分信息内容已被使用，请重新输入或联系管理员！")

                                            }
                                        }
                                    })
                                } else {
                                    layer.msg("您输入的部分信息内容已被使用，请重新输入或联系管理员！")

                                }
                            }
                        })
                    },
                    btn2: function () {
                        return;
                    },
                });
                return false;
            });
        }
    });

    function getAge(idCard) {
        //获取年龄
        // var idCard = "350103199712052315"
        var myDate = new Date();
        var month = myDate.getMonth() + 1;
        var day = myDate.getDate();
        var age = myDate.getFullYear() - idCard.substring(6, 10) - 1;
        if (idCard.substring(10, 12) < month || idCard.substring(10, 12) == month && idCard.substring(12, 14) <= day) {
            age++;
        }
        return age
    }

    function getRandomCardNo() {
        loginStatus = $.cookie("loginUserStatus")
        var Num = "";
        var cardNo = "";
        for (var i = 0; i < 6; i++) {
            Num += Math.floor(Math.random() * 10);
        }
        if (loginStatus == 0) {
            cardNo = "PT" + Num
        } else {
            cardNo = "GLY" + Num
        }
        console.log(cardNo)
        var res
        $.ajax({
            url: '/checkCardNo',
            type: 'post',
            data: {
                cardno: cardNo
            },
            success: function (resCardNo) {
                console.log(resCardNo)
                console.log(cardNo)
                if (resCardNo == '') {
                    res = 0
                }else{
                    res = 1
                }
            }
        })
        if (res == 1){
            getRandomCardNo()
        }else {
            return cardNo
        }
    }

    function addOptions(data, str) {
        var s = "#" + str
        var pro = $(s);
        pro.empty()
        var options = '<option value="" selected=""></option>';
        for (var i = 0; i <= data.length-1; i++) {
            options += '<option value="' + data[i] + '" >' + data[i] + '</option>';
        }
        pro.append(options);
    }

    function addOptions2(data, str, selectedItem) {
        var s = "#" + str
        var pro = $(s);
        pro.empty()
        var options = '<option value="" ></option>';
        for (var i = 0; i <= data.length - 1; i++) {
            if (data[i] == selectedItem) {
                options += '<option value="' + data[i] + '" selected="">' + data[i] + '</option>';
            } else {
                options += '<option value="' + data[i] + '" >' + data[i] + '</option>';
            }
        }
        pro.append(options);
    }
})
